package com.example.controller;

import com.example.domain.ResponseResult;
import com.example.domain.entity.User;
import com.example.enums.AppHttpCodeEnum;
import com.example.handler.exception.SystemException;
import com.example.service.AdminLoginService;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class LoginController {

    @Resource
    private AdminLoginService adminLoginService;
    //登录接口，通过JWT加密将userId生成Token发送到前端，将LoginUser存入redis中
    @PostMapping("/login")
    public ResponseResult login(@RequestBody User user){
        if (!StringUtils.hasText(user.getStudentid())){
            throw new SystemException(AppHttpCodeEnum.PASSWORD_ERROR);
        }
        if (!StringUtils.hasText(user.getPassword())){
            throw new SystemException(AppHttpCodeEnum.PASSWORD_ERROR);
        }
        return adminLoginService.login(user);
    }

    //登出接口，删除当前用户中的redis信息
    @PostMapping("/logout")
    public ResponseResult logout(){
        return adminLoginService.logout();
    }

}